﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapAMSTAMM(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveAMSTAMM(DataRow dwData, string ITXDBVERSION = "JLT")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryAMSTAMM(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapAMSTAMM(ref dwLocal, dwData);

            if (!flgExist)
            {
                InserAMSTAMM(dwLocal);
            }
            UpdateAMSTAMM(dwLocal);
        }
        public void InserAMSTAMM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            //APZMANDANT APZART APZVAR APZANRKRS APZALFDNR APZSTUFE
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();

            setResult = Connect2Proxy(or1);
        }
        public void UpdateAMSTAMM(DataRow dwData, int option=1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            { 
                case 0:
                    break;
                case 1: 
                    break;
                case 2:
                    or1.CommandText = "UPDATE AMSTAMM SET AMAUFTRVON=:AMAUFTRVON"
                    + " WHERE AMMANDANT=:AMMANDANT AND  AMNRKRS=:AMNRKRS AND AMLFDNR=:AMLFDNR AND AMPOS=:AMPOS "
                     ;
                    or1.Parameters.Add("AMMANDANT", OracleType.NVarChar).Value = dwData["AMMANDANT"].ToString();
                    or1.Parameters.Add("AMNRKRS", OracleType.NVarChar).Value = dwData["AMNRKRS"].ToString();
                    or1.Parameters.Add("AMLFDNR", OracleType.NVarChar).Value = dwData["AMLFDNR"].ToString();
                    or1.Parameters.Add("AMPOS", OracleType.NVarChar).Value = dwData["AMPOS"].ToString();
                    or1.Parameters.Add("AMAUFTRVON", OracleType.NVarChar).Value = dwData["AMAUFTRVON"].ToString();
                    break;
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryAMSTAMM(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;
            // APZMANDANT  APZART APZVAR APZANRKRS APZALFDNR APZSTUFE

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM AMSTAMM WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT M.*,'m'||RTRIM(AMNRKRS)||'_'||AMLFDNR||'_'||AMPOS AS MACHINEID,RTRIM(AMHERST) AS SMTYP FROM AMSTAMM M "
                    + " WHERE AMMANDANT=:AMMANDANT AND  AMKSTKRS=:AMKSTKRS AND AMKST=:AMKST AND AMTYP=:AMTYP "
                    + " ORDER BY AMNRKRS,AMLFDNR,AMPOS ";
                    or1.Parameters.Add("AMMANDANT", OracleType.NVarChar).Value = dwData["AMMANDANT"].ToString();
                    or1.Parameters.Add("AMKSTKRS", OracleType.NVarChar).Value = dwData["AMKSTKRS"].ToString();
                    or1.Parameters.Add("AMKST", OracleType.NVarChar).Value = dwData["AMKST"].ToString();
                    or1.Parameters.Add("AMTYP", OracleType.NVarChar).Value = dwData["AMTYP"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT  AMNRKRS||AMLFDNR||AMPOS AS MACHINEID,AMNRKRS,AMLFDNR,AMPOS,AMBEZ,AMSTKBIS, AMAUFTRVON, AMAUFTRBIS ,AMBDEMA FROM AMSTAMM M "
                    + " WHERE  AMNRKRS||AMLFDNR||AMPOS=:MACHINEID OR (RTRIM(AMART)||RTRIM(AMBDEMA)=RTRIM(:ABFAGANG)||RTRIM(:MACHINEID))"
                    + " ORDER BY AMNRKRS,AMLFDNR,AMPOS ";
                    or1.Parameters.Add("MACHINEID", OracleType.NVarChar).Value = dwData["MACHINEID"].ToString();
                    or1.Parameters.Add("ABFAGANG", OracleType.NVarChar).Value = dwData["ABFAGANG"].ToString();

                    break;
                case 3:
                    or1.CommandText = "SELECT  AMNRKRS||AMLFDNR||AMPOS AS MACHINEID,AMNRKRS,AMLFDNR,AMPOS,AMBEZ,AMSTKBIS,AMAUFTRVON,AMAUFTRBIS ,AMBDEMA FROM AMSTAMM M "
                    + " WHERE  RTRIM(AMART)||RTRIM(AMBDEMA)=RTRIM(:ABFAGANG)||RTRIM(:AMBDEMA)"
                    + " ORDER BY AMNRKRS,AMLFDNR,AMPOS ";
                    or1.Parameters.Add("AMBDEMA", OracleType.NVarChar).Value = dwData["AMBDEMA"].ToString();
                    or1.Parameters.Add("ABFAGANG", OracleType.NVarChar).Value = dwData["ABFAGANG"].ToString();

                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "AMSTAMM";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
